{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Estimates with tools producing QIR\n",
    "\n",
    "Azure Quantum Resource Estimation is built upon [QIR](https://www.qir-alliance.org/), the forward-looking, fully interoperable specification for quantum programs.  In this notebook, we are showing how to use the `azure.quantum` Python package to directly submit QIR to the Resource Estimation target.  We are using [PyQIR](https://github.com/qir-alliance/pyqir) to generate QIR, however the example works with any other source of QIR as well. PyQIR can help you as a library to generate QIR from other quantum programming languages, and thereby enabling their execution on Azure Quantum Resource Estimator."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Getting started\n",
    "\n",
    "We import several Python classes and functions from `azure.quantum` and `pyqir`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from azure.quantum import Workspace\n",
    "from azure.quantum.target.microsoft import MicrosoftEstimator\n",
    "\n",
    "# Support code to transition from pyqir-generator to pyqir package\n",
    "try:\n",
    "    from pyqir.generator import BasicQisBuilder, SimpleModule\n",
    "except:\n",
    "    from pyqir import BasicQisBuilder, SimpleModule"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We connect to the Azure Quantum workspace."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "workspace = Workspace(\n",
    "    resource_id = \"\",\n",
    "    location = \"\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We create an instance of the Resource Estimator in that workspace.  Make sure that you have the _Microsoft Quantum Computing_ provider added to the workspace."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "estimator = MicrosoftEstimator(workspace)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Running a sample quantum program\n",
    "\n",
    "Let's now create some QIR bitcode using PyQIR generator.  Here, we build a controlled S gate using 3 T gates and 2 CNOT gates."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "module = SimpleModule(\"Controlled S\", num_qubits=2, num_results=0)\n",
    "qis = BasicQisBuilder(module.builder)\n",
    "\n",
    "[a, b] = module.qubits[0:2]\n",
    "qis.t(a)\n",
    "qis.t(b)\n",
    "qis.cx(a, b)\n",
    "qis.t_adj(b)\n",
    "qis.cx(a, b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Before we submit the QIR to the resource estimator, let's take a look at the QIR\n",
    "output for this module.  We can use the `ir` function from PyQIR for that\n",
    "purpose, that generates human-readable instructions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(module.ir())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The QIR we submit to the function defined above must be passed in bitcode\n",
    "format.  We obtain this format by calling `bitcode` instead of `ir` on the\n",
    "module.  We can also pass resource estimation specific arguments, e.g., setting\n",
    "the error rate to 0.5%."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "params = estimator.make_params()\n",
    "params.error_budget = 0.005\n",
    "job = estimator.submit(module.bitcode(), input_params=params)\n",
    "result = job.get_results()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, we print the resource estimation table."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
